package com.zmj.sy.mom.srv.aps.shcedule;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.zmj.sy.mom.srv.aps.service.ApsWorkDetailService;
import com.zmj.sy.mom.srv.aps.service.ReportErpService;
import com.zmj.sy.mom.srv.aps.utils.DateUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;


@Slf4j
@Component
@RequiredArgsConstructor
@ConditionalOnProperty(name = "zmj.sy.serNumber", havingValue = "1")
public class ReportErpSchedule {
    private final ApsWorkDetailService apsWorkDetailService;
    private final ReportErpService reportErpService;
    /**
     * @Description: 每两个小时回传一次报工记录
     *
     * @author: hejiahui
     * @since: 2024/10/9 16:20
     */
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void syncReportData() {
        try {
            //报工记录转换为erp回传格式
            reportErpService.changeReportData();
            //按时间顺序回传近四小时erp
            String startTime = DateUtil.date().offset(DateField.HOUR,-4).toString("yyyy-MM-dd HH:mm:ss");
            String endTime = DateUtil.date().toString("yyyy-MM-dd HH:mm:ss");
            new Thread(() -> {
                reportErpService.reportErpDate(startTime,endTime);
            }).start();
        } catch (Exception e) {
            log.error("回传erp失败",e);
        }
    }
    /**
     * @Description: 每天重新回传近一个月失败的报工记录
     *
     * @author: hejiahui
     * @since: 2024/10/9 16:20
     */
    @Scheduled(cron = "0 30 4 * * ?")
    public void reportToErp() {
        try {
            new Thread(() -> {
                reportErpService.dynamicReportToErp();
            }).start();
        } catch (Exception e) {
            log.error("每天重新回传erp失败",e);
        }

    }

    /**
     * 定时生成日工组计划
     */
    @Scheduled(cron = "0 0 6 * * ?")
    public void makeMesWorkDetail() {
        try {
            // TODO 没啥用，直接废弃
            apsWorkDetailService.makeMesWorkDetail(DateUtils.format(new Date(), "yyyy-MM-dd"));
        } catch (Exception e) {
            log.error("生成日工组计划失败",e);
        }

    }
}
